﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarriorRush.GameDynamics.Utilities;
using WarriorRush.UserInterface;

namespace WarriorRush.GameDynamics
{
    public class SpecialAbility
    {
        public List<SimplePoint> Targets;
        public GameEffect Effect{ 
            get 
            {
                if(Effects.Count > 0)
                    return Effects.First().Value;
                return null;
            }
            set 
            {
                if(Effects.Count > 0)
                    Effects[Effects.First().Key] = value;
            }
        }
        public Dictionary<EffectType, GameEffect> Effects;
        public int Power;
        public CharacterClass AbilityOwner;
        public bool ReplacesAttack;
        public bool RequiresAddtionalTarget;
        public AbilityTypes AbilityType;
        public string ShortName;
        public string Description;
        public EffectType? EfType{
            get { 
                if(Effects.Count > 0) 
                    return Effects.First().Key;
                return null;
            }
            set
            {
                var currentEffect = EfType;
                if (!value.HasValue)
                {
                    if (!currentEffect.HasValue)
                        return;
                    else
                        Effects.Remove(currentEffect.Value);
                }
                else if (Effects.Count == 0)
                    Effects.Add(value.Value, null);
                else if(Effects.Count == 1)
                {
                    Effects.Remove(currentEffect.Value);
                    Effects.Add(value.Value, null);
                }
            }
        
        }
        
        public SpecialAbility()
        {

            Effects = new Dictionary<EffectType, GameEffect>();
        }

        public virtual bool Execute(CharacterClass abilityTarget, Arena arena, List<CharacterClass> additonalTargets)
        {
            //Should the player still deal normal damage
            return true;
        }

        public virtual SpecialAbility GetInstance(int power)
        {
            return new SpecialAbility() 
            {Power = power, ReplacesAttack = this.ReplacesAttack, ShortName = ShortName, Description = Description };
         
        }

        public virtual Selectables GetAddtionalTargetables(Arena arena)
        {
            return null;
        }

        public virtual SelectionHandler GetAllTargets(Arena arena, ArenaSquare target)
        {

            return new SelectionHandler()
            {
                Squares =
                   arena.GetTargetedSpaces(target, Targets, true)
                       .Select(f => new Selectable() { Square = f }).ToList(),
                SelectType = HighlightType.Targetable,
                IsReplacedSelection = true,
                MaxSelections = 1

            };
        }

        public virtual void CalculateTargets()
        {

        }

        public virtual List<CharacterClass> GetTargets(CharacterClass attacker, Arena arena)
        {
            return arena.GetTargetedSpaces(attacker.Square,Targets,true)
                    .Where(f=>f.Character != null)
                    .Select(f=>f.Character).ToList();
        }
    }

    public enum AbilityTypes
    {
        Cleave,
        Pierce,
        Blind,
        Heal,
        Range,
        Poison,
        Evasion,
        Silence,
        Enrage,
        Shield,
        Daze,
        WarCry,
        Patrol,
        Contaminate,
        Aggrovate,
        Curse,
        Hasten,
        HardenSkin

    }
}
